package com.agile.springboot.service.impl;

import com.agile.springboot.dao.GroupMapper;
import com.agile.springboot.dao.GroupRelationMapper;
import com.agile.springboot.dao.UserMapper;
import com.agile.springboot.entity.Group;
import com.agile.springboot.entity.GroupRelation;
import com.agile.springboot.entity.User;
import com.agile.springboot.service.GroupService;
import com.agile.springboot.service.UserService;
import com.agile.springboot.utils.DateUtil;
import com.agile.springboot.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service
public class GroupServiceImpl implements GroupService {

    @Resource
    GroupMapper groupMapper;
    @Resource
    GroupRelationMapper groupRelationMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    UserService userService;

    /**
     * 新增群组
     * @param group 群组实体
     * @return 返回值为1，则说明新增群组成功
     */
    @Override
    public int addGroup(Group group) {
        group.setId(UUIDUtil.getOneUUID());
        group.setCreation_date(DateUtil.getCurrentDate());
        group.setLast_update_date(DateUtil.getCurrentDate());
        group.setIs_deleted(0);
        return groupMapper.insert(group);
    }

    /**
     * 逻辑删除群组
     * @param group_id 群组id
     * @return 返回值为1，则说明逻辑删除群组成功
     */
    @Override
    public int delGroup(String group_id) {
        return groupMapper.deleteById(group_id);
    }

    /**
     * 物理删除群组
     * @param group_id 群组id
     * @return 返回值为1，则说明物理删除群组成功
     */
    @Override
    public int pscDelGroup(String group_id) {
        return groupMapper.pscDeleteById(group_id);
    }

    /**
     * 修改群组信息
     * @param group 群组实体
     * @return 返回值为1，则说明修改群组信息成功
     */
    @Override
    public int updateGroup(Group group) {
        group.setLast_update_date(DateUtil.getCurrentDate());
        return groupMapper.updateById(group);
    }

    /**
     * 模糊搜索群组
     * @param group_name 群组名
     * @return List<Group> 群组列表
     */
    @Override
    public List<Group> fuzzySearchGroupByName(String group_name) {
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(group_name!=null,"group_name", group_name);
        return groupMapper.selectList(queryWrapper);
    }

    /**
     * 精准搜索群组——根据群组名
     * @param group_name 群组名
     * @return Group 实体
     */
    @Override
    public Group exactSearchGroupByName(String group_name) {
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(group_name!=null,"group_name", group_name);
        return groupMapper.selectOne(queryWrapper);
    }

    /**
     * 精准搜索群组——根据群组id
     * @param group_id 群组id
     * @return Group 实体
     */
    @Override
    public Group searchGroupById(String group_id) {
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(group_id!=null,"id", group_id);
        return groupMapper.selectOne(queryWrapper);
    }

    /**
     * 查询当前用户创建的所有群组
     * @param user_id 用户id
     * @return List<Group> 该用户创建的所有群组
     */
    @Override
    public List<Group> queryGroupsCreatedByUser(String user_id){
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("created_by",user_id);
        return groupMapper.selectList(queryWrapper.allEq(map));
    }

    /**
     * 查询当前答者所属的所有群组——根据当前用户id
     * @param user_id 答者 id
     * @return List<Group> 该答者所属的所有群组
     */
    @Override
    public List<Group> queryGroupsBelongToAnswerer(String user_id){
        //查询包含当前答者id的群组关系
        QueryWrapper<GroupRelation> queryWrapper_groupRelation = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("member_id",user_id);
        List<GroupRelation> groupRelationList = groupRelationMapper.selectList(queryWrapper_groupRelation.allEq(map));
        //存储相应的群组
        List<Group> groupList = new ArrayList<>();
        for(GroupRelation groupRelation : groupRelationList){
            groupList.add(groupMapper.selectById(groupRelation.getGroup_id()));
        }
        return groupList;
    }

    /**
     * 向群组中添加答者
     * @param group_id 群组id
     * @param answerer_id 答者id
     * @return 返回值为1，则说明向群组中添加答者成功，-2说明输入id所对应的不是答者，-2说明该答者已经在群组中存在
     */
    @Override
    public int addAnswererToGroup(String group_id,String answerer_id){
        Date date = new Date();
        GroupRelation groupRelation = new GroupRelation();
        groupRelation.setGroup_id(group_id);
        groupRelation.setMember_id(answerer_id);
        groupRelation.setJoin_time(date);
        //用于查询数据库表中是否已经存在该群组关系
        QueryWrapper<GroupRelation> queryWrapper_groupRelation = new QueryWrapper<>();
        Map<String,Object> map_groupRelation = new HashMap<>();
        map_groupRelation.put("group_id",group_id);
        map_groupRelation.put("member_id",answerer_id);
        //判断当前输入的答者id是否为答者
        if(userService.searchUserById(answerer_id).getIdentity()!=3){
            return -2;
        }else if(groupRelationMapper.selectList(queryWrapper_groupRelation.allEq(map_groupRelation)).size()!=0){
            return -1;
        }else{
            return groupRelationMapper.insert(groupRelation);
        }
    }

    /**
     * 删除群组中的某一答者
     * @param group_id 群组id
     * @param answerer_id 答者id
     * @return 返回值为1，则说明删除群组中的该答者成功
     */
    @Override
    public int deleteAnswerersFromGroup(String group_id, String answerer_id){
        QueryWrapper<GroupRelation> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("group_id",group_id);
        map.put("member_id",answerer_id);
        return groupRelationMapper.delete(queryWrapper.allEq(map));//将群组id和答者id都相同的群组关系删除
    }

    /**
     * 查询出该群组的所有答者id——根据群组id
     * @param group_id
     * 所必须的参数为群组group的id
     * @return List<String> 答者id的集合
     */
    @Override
    public List<String> getUsersIdInGroup(String group_id){
        QueryWrapper<GroupRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(group_id!=null,"group_id", group_id);
        List<GroupRelation> list = groupRelationMapper.selectList(queryWrapper);
        List<String> listMemberId = new ArrayList<>();
        Iterator<GroupRelation> iterator = list.iterator();
        for(int i=0;iterator.hasNext();i++){
            listMemberId.add(list.get(i).getMember_id());
            iterator.next();
        }
        return listMemberId;
    }

    /**
     * 查询出不在该群组的所有答者id——根据群组id
     * @param group_id 群组id
     * @return List<User> 答者的集合
     */
    @Override
    public List<User> getUsersIdNotInGroup(String group_id){
        //获取所有在当前群组中的答者所在的群组关系
        QueryWrapper<GroupRelation> queryWrapper_groupRelation = new QueryWrapper<>();
        queryWrapper_groupRelation.eq(group_id!=null,"group_id", group_id);
        List<GroupRelation> groupRelationList = groupRelationMapper.selectList(queryWrapper_groupRelation);
        //获取用户列表中的所有用户或答者
        QueryWrapper<User> queryWrapper_answer = new QueryWrapper<>();
        List<User> userList = userMapper.selectList(queryWrapper_answer.eq("identity",3));
        //存储不在当前群组中的答者id
        List<User> memberList = new ArrayList<>();
        for(User user : userList){
            boolean is = false;//判断当前用户或答者是否已经在该群组中
            for(GroupRelation groupRelation : groupRelationList){
                if(groupRelation.getMember_id().equals(user.getId())){
                    is = true;
                }
            }
            if(!is){
               memberList.add(user);
            }
        }
        return memberList;
    }

    /**
     * 查询当前答者加入该群组的时间
     * @param group_id 群组id
     * @param member_id 答者id
     * @return Date 时间
     */
    @Override
    public Date getUserJoinTime(String group_id, String member_id){
        QueryWrapper<GroupRelation> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("group_id",group_id);
        map.put("member_id",member_id);
        return groupRelationMapper.selectOne(queryWrapper.allEq(map)).getJoin_time();
    }
}
